package scau.redi.social_platform.controller.user;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import scau.redi.social_platform.common.staticCode.Const;
import scau.redi.social_platform.common.staticCode.ResponseCode;
import scau.redi.social_platform.common.util.ResponsePageUtil;
import scau.redi.social_platform.common.util.ResponseUtil;
import scau.redi.social_platform.domain.pojo.Shared;
import scau.redi.social_platform.domain.pojo.SharedContent;
import scau.redi.social_platform.domain.pojo.SharedImg;
import scau.redi.social_platform.domain.pojo.UserInfo;
import scau.redi.social_platform.domain.vo.BriefSharedVO;
import scau.redi.social_platform.service.SharedContentService;
import scau.redi.social_platform.service.SharedImgService;
import scau.redi.social_platform.service.SharedService;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author redi
 * @since 2022-05-05
 */
@RestController
@Api(value = "shared",description = "分享")
@RequestMapping("/shared")
public class SharedController {

    @Autowired
    SharedService sharedService;
    @Autowired
    SharedContentService sharedContentService;
    @Autowired
    SharedImgService sharedImgService;


    @PostMapping("/create.do")
    @ApiOperation("发布分享（普通用户、管理员）")
    public Map<String, Object> createShared(@RequestParam(value = "content", required = false)String content,
                                            @RequestParam(value = "uploadPath", required = false)String uploadPath,
                                            @RequestParam(value = "statusId", required = false, defaultValue = "2")Integer statusId,
                                            HttpSession session
    )
    {
        // 检查权限
        UserInfo user = (UserInfo) session.getAttribute(Const.CURRENT_USER);
        if (null == user){
            return ResponseUtil.fail(ResponseCode.NEED_LOGIN.getDesc(),ResponseCode.NEED_LOGIN.getCode());
        }
        // 检查user status字段
        if (user.getStatusId() == Const.UserStatus.STATUS_BAN){
            return ResponseUtil.fail(ResponseCode.BAN.getDesc(),ResponseCode.BAN.getCode());
        }

        // 检查内容是否为空
        if ((StringUtils.isAllBlank(content,uploadPath))){
            return ResponseUtil.fail("请先输入文字内容或上传图片");
        }

        // Shared 插入
        Shared shared = new Shared();
        shared.setUserId(user.getId());
        shared.setStatusId(statusId);
        shared.setCreateTime(new Date());
        shared.setUpdateTime(new Date());
        int createSharedResultCode = sharedService.createShared(shared);    //返回值：插入失败则为0， 否则为插入行序号
        if (createSharedResultCode==0){
            return ResponseUtil.fail("shared 插入失败");
        }

        // SharedContent 插入
        if (null != content) {
            SharedContent sharedContent = new SharedContent();
            sharedContent.setContent(content);
            sharedContent.setSharedId(createSharedResultCode);     // 插入行序号赋值给 sharedContent
            sharedContent.setCreateTime(new Date());
            sharedContent.setUpdateTime(new Date());

            int createContentResultCode = sharedContentService.createsharedContent(sharedContent);
            if (createContentResultCode==0){
                return ResponseUtil.fail("sharedContent 插入失败");
            }
        }

        //image 插入
        if (null != uploadPath){
            SharedImg sharedImg = new SharedImg();
            sharedImg.setSharedId(createSharedResultCode);
            sharedImg.setImgUrl(uploadPath);
            sharedImg.setCreateTime(new Date());
            sharedImg.setUpdateTime(new Date());

            int createSharedImgResultCode = sharedImgService.createSharedImg(sharedImg);
            if (createSharedImgResultCode == 0){
                return ResponseUtil.fail("sharedImg 插入失败");
            }
        }

        return ResponseUtil.success(sharedService.findById(createSharedResultCode));

    }

    @GetMapping("/delete.do")
    @ApiOperation("删除分享（普通用户、管理员）")
    public Map<String, Object> deleteShared(@RequestParam(value = "sharedId", required = true)int sharedId,
                                            HttpSession session)
    {
        // 检查权限
        UserInfo user = (UserInfo) session.getAttribute(Const.CURRENT_USER);
        if (null == user){
            return ResponseUtil.fail(ResponseCode.NEED_LOGIN.getDesc(),ResponseCode.NEED_LOGIN.getCode());
        }
        // 删除
        int deleteSharedResultCode = sharedService.deleteShared(sharedId);    //返回值：插入失败则为0， 否则为插入行序号
        if (deleteSharedResultCode == 0){
            return ResponseUtil.fail("shared 删除失败");
        }
        return ResponseUtil.success("shared 删除成功");
    }

    @GetMapping("/update.do")
    @ApiOperation("修改分享状态（普通用户、管理员）")
    public Map<String, Object> updateShared(@RequestParam(value = "sharedId", required = true)int sharedId,
                                            @RequestParam(value = "statusId", required = true)int statusId,
                                            HttpSession session)
    {
        // 检查权限
        UserInfo user = (UserInfo) session.getAttribute(Const.CURRENT_USER);
        if (null == user){
            return ResponseUtil.fail(ResponseCode.NEED_LOGIN.getDesc(),ResponseCode.NEED_LOGIN.getCode());
        }
        // 检查user status字段
        if (user.getStatusId() == Const.UserStatus.STATUS_BAN){
            return ResponseUtil.fail(ResponseCode.BAN.getDesc(),ResponseCode.BAN.getCode());
        }

        // 更新
        Shared shared = new Shared();
        shared.setId(sharedId);
        shared.setStatusId(statusId);

        int updateSharedResultCode = sharedService.updateShared(shared);    //返回值：插入失败则为0， 否则为插入行序号
        if (updateSharedResultCode == 0){
            return ResponseUtil.fail("shared 状态更新失败或该shared不存在");
        }
        return ResponseUtil.success("shared 状态更新成功");
    }

    @GetMapping("/selectAll.do")
    @ApiOperation("获取所有分享（分页）（游客、普通用户、管理员）")
    public Map<String, Object> selectAll(@RequestParam(value = "page",defaultValue = "1")int page,
                                         @RequestParam(value = "size",defaultValue = "5")int size)
    {
        ResponsePageUtil<BriefSharedVO> sharedWithContentVoList = sharedService.findAllByPage(page,size);
        return ResponseUtil.success(sharedWithContentVoList);
    }

    @GetMapping("/selectSharedDetail.do")
    @ApiOperation("获取单个分享详情（游客、普通用户、管理员）")
    public Object selectSharedDetail(@RequestParam(value = "sharedId",required = true)int sharedId)
    {
        return ResponseUtil.success(sharedService.findDetailById(sharedId));
    }

//    @GetMapping("/getLikedRank.do")
//    @ApiOperation("获取点赞榜（游客、普通用户、管理员）")
//    public Map<String, Object> getLikedRank(@RequestParam(value = "page",defaultValue = "1")int page,
//                                            @RequestParam(value = "size",defaultValue = "5")int size)
//    {
//        ResponsePageUtil<BriefSharedVO> allByLikedSum = sharedService.findAllByLikedSum(page, size);
//        return ResponseUtil.success();
//    }

    @GetMapping("/selectAllByTime.do")
    @ApiOperation("获取分享, 按时间倒序（游客、普通用户、管理员）")
    public Map<String, Object> selectAllByTime(@RequestParam(value = "page",defaultValue = "1")int page,
                                                @RequestParam(value = "size",defaultValue = "5")int size)
    {
        return ResponseUtil.success(sharedService.findAllByTime(page, size));
    }

    @GetMapping("/getComment.do")
    @ApiOperation("获取分享的所有评论（游客、普通用户、管理员）")
    public Map<String, Object> selectAll(@RequestParam(value = "page",defaultValue = "1")int page,
                                         @RequestParam(value = "size",defaultValue = "5")int size,
                                         @RequestParam(value = "sharedId")int sharedId)
    {
        List sharedComment = sharedService.getSharedComment(page, size, sharedId);
        return ResponseUtil.success(sharedComment);
    }

}

